/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package dinner.web;

import java.util.List;

import javax.jws.Oneway;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;

import dinner.web.eat.MenuDB;
import dinner.web.eat.MenuItem;
import dinner.web.eat.ReportIO;
import dinner.web.secure.AutorisingSystem;
import dinner.web.secure.UnAutorizationException;

/**
 * 
 * @author Bizon
 */
@WebService()
public class DinnerService {

	/**
	 * Web service operation
	 */
	@WebMethod(operationName = "login")
	public Long login(@WebParam(name = "login") String login, @WebParam(name = "pass") String pass) throws UnAutorizationException {
		return DinnerDAO.login(login, pass);
	}

	/**
	 * Web service operation
	 */
	@WebMethod(operationName = "getList")
	public java.util.List<Eat> getList(@WebParam(name = "id") long id) throws UnAutorizationException {
		return DinnerDAO.getList(id);
	}

	/**
	 * Web service operation
	 */
	@WebMethod(operationName = "setList")
	public void setList(@WebParam(name = "list") List<Eat> list, @WebParam(name = "id") long id) throws UnAutorizationException {
		DinnerDAO.setList(list, id);
	}

	/**
	 * Web service operation
	 */
	@WebMethod(operationName = "getMenuList")
	public List<MenuItem> getMenuList(@WebParam(name = "id") long id) throws UnAutorizationException {
		AutorisingSystem.checkAdmin(id);
		return MenuDB.getListForToday();
	}

	/**
	 * Web service operation
	 */
	@WebMethod(operationName = "setMenu")
	public void setMenu(@WebParam(name = "id") long id, @WebParam(name = "menu") List<MenuItem> menu) throws UnAutorizationException {
		AutorisingSystem.checkAdmin(id);
		MenuDB.setListForToday(menu);
	}

	/**
	 * Web service operation
	 */
	@WebMethod(operationName = "logout")
	@Oneway
	public void logout(@WebParam(name = "id") long id) {
		AutorisingSystem.logout(id);
	}

	/**
	 * Web service operation
	 */
	@WebMethod(operationName = "closeDay")
	public void closeDay(@WebParam(name = "id") long id) throws UnAutorizationException {
		ReportIO.closeUser(AutorisingSystem.isLogined(id));
	}

	/**
	 * Web service operation
	 */
	@WebMethod(operationName = "getReport")
	public List<ReportLine> getReport(@WebParam(name = "id") long id) throws UnAutorizationException {
		AutorisingSystem.checkAdmin(id);
		return ReportIO.getReport();
	}

	/**
	 * Web service operation
	 */
	@WebMethod(operationName = "clearData")
	public void clearData(@WebParam(name = "id") long id) throws UnAutorizationException {
		try {
			AutorisingSystem.checkAdmin(id);
			DinnerDAO.clearAllLists();
		} catch (UnAutorizationException e) {
			String name = AutorisingSystem.isLogined(id);
			DinnerDAO.clearList(name);
		}
	}

}
